package com.comtime.usercenter;

import java.io.IOException;
import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;

import android.os.Bundle;
import android.test.UiThreadTest;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.comtime.greendaodemo.DeviceInfo;
import com.comtime.greendaoutils.DataBaseUtil;
import com.comtime.smartbox.BaseActivity;
import com.comtime.smartkilimanjaro.R;
import com.comtime.utils.LogUtils;
import com.comtime.utils.MyConfig;
import com.comtime.utils.MySharedPreferences;
import com.comtime.utils.ToastUtils;
import com.comtime.view.SelectDevicePopupWindow;
import com.comtime.view.SelectDevicePopupWindow.OnDeviceSelectListener;
import com.comyou.comyouhttp.ComyouHttpCallBack;
import com.comyou.comyouhttp.ComyouHttpClient;
import com.comyou.comyouhttp.ComyouHttpProgram;

public class AuthActivity extends BaseActivity implements OnDeviceSelectListener{
	
	
	MySharedPreferences mySharedPreferences;
	
	String[] macs;
	
	private TextView tv_mac;
	public void backAction(View v){
		this.finish();
		overridePendingTransition(R.anim.enter_left, R.anim.exit_right);
	}
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_auth);
		mySharedPreferences=MySharedPreferences.getInstance(this);
		List<DeviceInfo> deviceInfos = DataBaseUtil.getInstance(this).loadDeviceInfo(mySharedPreferences
				.getPhoneNum());
		macs=new String[deviceInfos==null? 0:deviceInfos.size()];
		
		for (int i = 0; i < macs.length; i++) {
			DeviceInfo deviceInfo =deviceInfos.get(i);
			
			macs[i]=deviceInfo.getMac();
		}
	 
		tv_mac=(TextView) findViewById(R.id.tv_mac);
	}
	
	
	public void sendAuthAction(View v){
		String mac=tv_mac.getText().toString();
		
		String phone=((EditText)findViewById(R.id.edit_phone)).getText().toString();
		String pwd=((EditText)findViewById(R.id.edit_pwd)).getText().toString();
		
		
		if(mac==null ||mac.length()<1){
			ToastUtils.showToast(this, "请选择要授权的设备", 1500);
			return;
		}
		if(pwd==null ||pwd.length()<1){
			ToastUtils.showToast(this, "请输入授权设备的管理密码", 1500);
			return;
		}
		
		if(pwd==null ||pwd.length()<1){
			ToastUtils.showToast(this, "请输入正确的被授权人的手机号码", 1500);
			return;
		}
		ComyouHttpProgram httpProgram = new ComyouHttpProgram();
		//httpProgram.add("phoneNum", mySharedPreferences.getPhoneNum());
		httpProgram.add("user_id", mySharedPreferences.getUserId()+"");
		httpProgram.add("mac", mac);
		httpProgram.add("sub_user_phone", phone);
		httpProgram.add("sub_user_password", pwd);
		
		
		LogUtils.e("tag", " httpProgram:"+httpProgram.getPrograms().toString());
		
		ComyouHttpClient httpClient = new ComyouHttpClient(MyConfig.IP
				+ "app/Login/addChildAccount");
		httpClient.postAsync(httpProgram, new ComyouHttpCallBack() {
			@Override
			public void onResponse(final String string) {
				Log.e("tag", "授权用户：" + string);
 
				try {
					JSONObject jsonObject = new JSONObject(string);
					int err = jsonObject.getInt("err");
					if (err == 0) {
//						JSONObject dataObject = jsonObject
//								.getJSONObject("data");
						
					}

				} catch (JSONException e) {
				 
				}
			}

			@Override
			public void onFailure(String arg0, IOException arg1) {
				 LogUtils.e("tag", "授权用户："+arg1  );
			}
		});
	}
	SelectDevicePopupWindow popupWindow;
	public void selectDeviceAction(View v){
		if(popupWindow==null){
			  popupWindow=new SelectDevicePopupWindow(this, this, macs);
		}
		
		popupWindow.showAsDropDown(findViewById(R.id.layout_select_device));
	}


	@Override
	public void onSelected(int index, String mac) {
		tv_mac.setText(mac);
		
	}
	
	
	
	
	
}
